package top.dvvj.wheelmall.boot.starter.config;

import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 线程池配置
 *
 * @author Dvj
 * @date 2021/8/4 16:45
 */
@Configuration
@EnableConfigurationProperties({ThreadPoolConfigurationProperties.class})
public class ThreadPoolConfiguration {

    @Bean
    @ConditionalOnMissingBean
    public ThreadPoolExecutor threadPoolExecutor(ThreadPoolConfigurationProperties pool) {
        return new ThreadPoolExecutor(pool.getCoreSize(), pool.getMaxSize(),
                pool.getKeepAliveTime(), TimeUnit.SECONDS,
                new LinkedBlockingDeque<>(pool.getQueueSize()),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy()
        );
    }
}
